package com.shanghai.market.plshnaghai.abstracts;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.shanghai.market.plshnaghai.net.VolleyParams;
import com.shanghai.market.plshnaghai.publics.LogCat;

/**
 * Created by Administrator on 2017/8/15.
 */

public  abstract class AbstractModelGet {

    private VolleyParams params;

    public VolleyParams getParams() {

        return params;
    }

    public void setParams(VolleyParams params) {
        this.params = params;
    }

    /**
     * 根据url获取网络数据
     *
     * @param url
     */
    public abstract void getData(String... args);

    /**
     * 请求失败
     *
     * @param message
     */
    protected abstract void error(String message);

    /**
     * 请求成功
     *
     * @param result
     */
    protected abstract void success(String result);

    protected Response.ErrorListener errorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error instanceof TimeoutError) {
                    error("网络连接超时！");
                } else if (error instanceof ServerError) {
                    error("连接服务器异常！");
                } else if (error instanceof AuthFailureError) {
                    error("连接服务器异常！");
                } else if (error instanceof NetworkError) {
                    error("网络未连接！");
                } else if (error instanceof NoConnectionError) {
                    error("网络未连接！");
                } else {
                    error("连接服务器异常！");
                }
            }
        };
    }

    protected Response.Listener<String> lisenter() {
        final Response.Listener<String> listener = new Response.Listener<String>() {

            @Override
            public void onResponse(String result) {
                LogCat.i(" AbstractModelGet success:" + result);
                success(result);
            }

        };
        return listener;
    }

    /**
     * relogin
     *
     * @param message
     * @param messageCode
     * @return
     */
//    protected boolean relogin(String message, String messageCode) {
//        if ("99091111".equals(messageCode)) {
////            ToastUtils.show(message);
////            Utils.startLoginActivity();
//            return true;
//        } else {
//            return false;
//        }
//    }
}
